//
//  MainViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "MainViewController.h"
#import "DrawerViewController.h"
#import "InformationViewController.h"
#import "MyInformationViewController.h"
#import "MainCollectionViewCell.h"
#import <QuartzCore/QuartzCore.h>
#import "NoNetWorkingView.h"
#import "OperationAnalysisViewController.h"
#import "AddressBookTwoViewController.h"





@interface MainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    BOOL firstCell;
    UITableView *table;
    UICollectionView *collection1;
    NSDictionary *UnreadNum;
    NoNetWorkingView *netWorkView;
}

@property (nonatomic,assign) BOOL isFirstLoadCell;
@property (nonatomic,strong) YALSunnyRefreshControl *sunnyRefreshControl;
@end
@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated{
      //注册管擦者，接受网络状态观察者发送过来的关于网络的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNoNetWorkTag:) name:@"noNetWork" object:nil];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.164 green:0.657 blue:0.915 alpha:1.000]];
}
- (void)viewWillDisappear:(BOOL)animated{
    //接受网络状态的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noNetWork" object:nil];
//    self.navigationController.navigationBar.translucent = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
     [self getUnreadNum];
    [self setUI];
}

//获取未读信息数
- (void)getUnreadNum{
    NSString *ashx = @"/Server/TotalInfoHandler.ashx";
    NSDictionary *dic = @{@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [LBProgressHUD showHUDto:self.view animated:YES];
    
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"未读信息：%@",model);
                UnreadNum = model[@"data"][0];
                [self setValue];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            NSLog(@"错误%@",error);
            [XHToast showBottomWithText:@"连接超时"];
            
        }
        [self endAnimationHandle];
        [LBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
    }];
}
//刷新控件
- (void)setValue{
    [collection1 reloadData];
}

//根据网络状态观察者传来的值进行判断，如果为－1就显示控件
- (void)showNoNetWorkTag:(NSNotification *)sender{
    NSString *isConnection = [sender object];
    if ([isConnection isEqualToString:@"-1"]) {
        [netWorkView setHidden:NO];
    }else{
        //有网络，隐藏控件，并重新发送请求数据
        [self getUnreadNum];
        [netWorkView setHidden:YES];
    }
    
    
}

- (void)setUI{
    UIButton* leftBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"菜单"] forState:UIControlStateNormal];
    leftBtn.frame = CGRectMake(0, 0, 25, 25);
    UIBarButtonItem* leftBtnItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(leftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setLeftBarButtonItem:leftBtnItem];
    UIImageView *backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"主界面(背景)"]];
    backImage.backgroundColor = [UIColor redColor];
    backImage.frame = CGRectMake(0, 0, KSWidth, KSHeight-60);
    [self.view addSubview:backImage];
    //配置collection view的布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //全局设置每一个item的尺寸
    layout.itemSize = CGSizeMake(KSWidth/2-10, KSWidth/2-10);
    //指定item之间的最小间隔。
    layout.minimumInteritemSpacing = 0;
    //指定最小行距
    layout.minimumLineSpacing = 0;
    
    //根据配置类初始化collection view
    collection1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight) collectionViewLayout:layout];
    
    collection1.backgroundColor = [UIColor clearColor];
//    collection1.alpha = 0;
    //内偏移量
    
    collection1.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    collection1.dataSource = self;
    collection1.delegate = self;
    //注册cell
    [collection1 registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collection1];
    collection1.alwaysBounceVertical = YES;
    [self setupRefreshControl];
    
    netWorkView = [[NoNetWorkingView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, 35)];
    [netWorkView setHidden:YES];
    [collection1 addSubview:netWorkView];
    
}



#pragma mark -- UICollectionViewDataSource
//配置collection view 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cell";
    MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    NSArray *imageArray = @[@"1",@"会议通知2",@"3",@"4",@"5",@"6"];
    NSArray *titleArray = @[@"公告通知",@"会议管理",@"事务审批",@"任务进程",@"通讯录",@"运营分析"];
    NSArray *UnreadArrayKey = @[@"news",@"meeting",@"flow",@"task",@"",@""];
    if (indexPath.row >3 ) {
        cell.num.hidden = YES;
    }else{
        cell.num.hidden = NO;
        
    }
    [cell configWithImage:[UIImage imageNamed:imageArray[indexPath.row]] title:titleArray[indexPath.row] unreadNum:UnreadNum[UnreadArrayKey[indexPath.row]]];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    switch (row) {
        case 1:{
            
            //会议通知  当为1时，说明点击了会议管理 ，会议管理没有分类，所以直接显示整个列表
            MyInformationViewController *viewController = [[MyInformationViewController alloc]init];
            viewController.title = @"会议管理";
            viewController.fromView = [NSString stringWithFormat:@"%ld",row+1];
            [self.navigationController pushViewController:viewController animated:YES];

        }
            break;
        case 4:{
            
            //通讯录
            AddressBookTwoViewController *addressBook = [[AddressBookTwoViewController alloc]init];
            [self.navigationController pushViewController:addressBook animated:YES];
            
        }
            break;
        case 5:{
            
            //运营分析
            OperationAnalysisViewController *yunying = [[OperationAnalysisViewController alloc] init];
            yunying.view.backgroundColor = [UIColor whiteColor];
            [self.navigationController pushViewController:yunying animated:YES];
            
        }
            break;
            
        default:{
        
            //其他菜单
            InformationViewController *ViewController = [[InformationViewController alloc]init];
            ViewController.fromView = [NSString stringWithFormat:@"%ld",row+1];
            [self.navigationController pushViewController:ViewController animated:YES];
        
        }
            break;
    }
    
}




/**
 *  下拉刷新控件初始化
 */
-(void)setupRefreshControl{
    self.sunnyRefreshControl = [YALSunnyRefreshControl new];
    [self.sunnyRefreshControl addTarget:self
                                 action:@selector(sunnyControlDidStartAnimation)
                       forControlEvents:UIControlEventValueChanged];
    [self.sunnyRefreshControl attachToScrollView:collection1];
    [self.sunnyRefreshControl beginRefreshing];
}
//刷新执行事件
-(void)sunnyControlDidStartAnimation{
    
    [self getUnreadNum];
    
}
//结束刷新控件动画
-(void)endAnimationHandle{
    
    [self.sunnyRefreshControl endRefreshing];
}
//开始下拉刷新动画
-(void)beginAnimationHandle{
    [self.sunnyRefreshControl beginRefreshing];
    
}


@end
